package com.bookRecommend.service.impl;

import com.bookRecommend.mapper.ITypeMapper;
import com.bookRecommend.model.Type;
import com.bookRecommend.service.ITypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TypeServiceImpl implements ITypeService {

    @Autowired
    ITypeMapper mapper;

    @Override
    public List<Type> findAllTypes() {
        return mapper.selectAllTypes();
    }

    @Override
    public Type findById(Integer id) {
        if (id == null) {
            return null;
        }
        return mapper.selectById(id);
    }

    @Override
    public int removeById(Integer id) {
        if (id == null) {
            return 0;
        }
        return mapper.deleteById(id);
    }

    @Override
    public int modifyById(String typeName, Integer id) {
        if (id == null || typeName == null || typeName.length() == 0) {
            return 0;
        }
        Type type = new Type();
        type.setId(id);
        type.setTypeName(typeName);
        return mapper.updateType(type);
    }

    @Override
    public int addType(String typeName) {
        if (typeName == null || typeName.length() == 0) {
            return 0;
        }
        Type type = new Type();
        type.setTypeName(typeName);
        return mapper.insertType(type);
    }

    @Override
    public PageInfo<Type> findPage(Integer n, Integer size) {
        if (n == null) {
            n = 1;
        }
        if (size == null) {
            size = 5;
        }
        PageHelper.startPage(n, size);
        return new PageInfo<>(mapper.selectPage());
    }


}
